package leetcode.editor.cn;
 
/**
 * 粉刷房子
 * @author qulq
 * @date 2024-07-28 21:15:44
 */
class TLCR091_JEj789{
	 public static void main(String[] args) {
	 	 // 测试代码
	 	 Solution solution = new TLCR091_JEj789().new Solution();
	 }

	/**
	 * 设油漆前i栋房子 并且房子i-1是红色、蓝色、绿色的最小花费分别为 f[i][0]、f[i][1]、f[i][2]
	 * f[i][0] = min(f[i-1][1] +  cost[i-1][0], f[i-1][2] +  cost[i-1][0])
	 * f[i][1]、f[i][2] 同理
	 */
// 力扣代码
//leetcode submit region begin(Prohibit modification and deletion)
class Solution {
    public int minCost(int[][] costs) {

		int[][] dp = new int[costs.length + 1][3];
		// dp[0][0]、dp[0][1]、dp[0][2] 默认为0
		for (int i = 1; i <= costs.length; i++) {
			for (int j = 0; j < 3; j++) {
				dp[i][j] = Integer.MAX_VALUE;
				for (int k = 0; k < 3; k++) {
					if (k == j) {
						continue;
					}
					dp[i][j] = Math.min(dp[i][j], dp[i - 1][k] + costs[i - 1][j]);
				}
			}
		}
		int ans = Integer.MAX_VALUE;
		for (int cost : dp[costs.length]) {
			ans = Math.min(ans, cost);
		}
		return ans;
    }
}
//leetcode submit region end(Prohibit modification and deletion)

}